﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using RelationTextBox;
using System.Data.SqlClient;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.IO;
using System.Reflection;


namespace ERPForms
{
    public partial class BalconSetupFrm : FrmCard
    {
        public BalconSetupFrm(ModuleClass mInfo)
            : base(mInfo)
        {
            InitializeComponent();
            this.relationTextBox1.ButtonClick += new EventHandler(relationTextBox1_ButtonClick);
            unitatiSursaBindingSource.Add(new UnitatiSursa("Comerciale", 0, "C "));
            unitatiSursaBindingSource.Add(new UnitatiSursa("Bugetare", 0, "B "));
            unitatiSursaBindingSource.Add(new UnitatiSursa("Non profit", 0, "NP"));
            workConnString = mInfo.workConnectionString;
            this.eSRPDataSet.BalconSetup.PrimaryKeyColumn.DefaultValue = 0;
            this.eSRPDataSet.BalconSetup.CodUnitateColumn.DefaultValue = ModuleInfo.CodUnitate;
            this.eSRPDataSet.BalconSetup.FolosesteJurnalColumn.DefaultValue = false;
            this.eSRPDataSet.BalconSetup.FolosesteNotaColumn.DefaultValue = false;
            this.eSRPDataSet.BalconSetup.FolosesteObservatiiColumn.DefaultValue = false;
            this.eSRPDataSet.BalconSetup.FolosesteValuteColumn.DefaultValue = false;
            this.eSRPDataSet.BalconSetup.DocumentJustifRulajColumn.DefaultValue = false;
            this.eSRPDataSet.BalconSetup.LunaCurentaColumn.DefaultValue = 1;
            this.eSRPDataSet.BalconSetup.AnCurentColumn.DefaultValue = 2010;
            this.eSRPDataSet.BalconSetup.CopiazaExplicatiaColumn.DefaultValue = true;
            this.eSRPDataSet.BalconSetup.SoldPrecedentActivColumn.DefaultValue = false;
            this.eSRPDataSet.BalconSetup.DataBlocareStartColumn.DefaultValue = DateTime.Now;
            this.eSRPDataSet.BalconSetup.FaraAdaugareGridColumn.DefaultValue = true;
            this.eSRPDataSet.BalconSetup.NoteInchidereUniceColumn.DefaultValue = true;
            this.eSRPDataSet.BalconSetup.TipUnitateColumn.DefaultValue = "C ";
            this.eSRPDataSet.BalconSetup.LiniiSemnaturaColumn.DefaultValue = true;
			this.eSRPDataSet.BalconSetup.FolosesteSursaFinantareColumn.DefaultValue = false;
            this.eSRPDataSet.BalconSetup.SolduriInitialeDefalcateJurnaleColumn.DefaultValue = false;
            this.eSRPDataSet.BalconSetup.FolosesteSectorBugetarColumn.DefaultValue = false;
			this.eSRPDataSet.BalconSetup.NivelAfisareConturiColumn.DefaultValue = 0;
        }

        void relationTextBox1_ButtonClick(object sender, EventArgs e)
        {
            FrmMainMdi frm = staticMdiParent as FrmMainMdi;
            Assembly asm = frm.GetModule("erpforms");
            if (asm == null)
                return;
            String numeClasa = "ERPForms.SeriiNumericeRezumatFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmRezumat newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                     new object[] { ModuleInfo }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmRezumat;
                newFrm.SelectionAddOn = true;
                //newFrm.MdiParent = frm;
                if (newFrm.ShowDialog() == DialogResult.OK)
                {
                    //daca s-a selectat un rand se pune in text box valoare
                    ((RelationTextBox.RelationTextBox)sender).Text = newFrm.selectedRow.Cells["IdSerieColumn"].Value.ToString();
                }

            }
        }

        private void BalconSetupFrm_Load(object sender, EventArgs e)
        {
            AddSource(balconSetupBindingSource, this.balconSetupTableAdapter.Adapter);

            if (!EsteArhiva())
            {
                if (ModuleInfo.CurrentTheme == "tribunal")
                {
                    AddButton("Executie Bugetara", global::ERPStudio.Properties.Resources._001_09, Executie_Bugetara_Setup, ToolStripItemAlignment.Right, false);
                }
                AddButton("Salveaza", global::ERPStudio.Properties.Resources.salvare1, Salvare, ToolStripItemAlignment.Right, true);
                AddButton("Inchidere An", global::ERPStudio.Properties.Resources._001_41, Inchidere_An, ToolStripItemAlignment.Right, false);
                HandleSalvare += new EventHandler(Salvare);
            }
            
            SetCurentUnit(ModuleInfo.CodUnitate);
        }


        private void Executie_Bugetara_Setup(object sender, EventArgs e)
        {
            FrmMainMdi frm = ModuleInfo.mdiForm as FrmMainMdi;
            Assembly asm = frm.GetModule("exbug");
            if (asm == null)
                return;
            String numeClasa = "ExBug.ExBugSetupFrm";
            Type mModule = asm.GetType(numeClasa);

            if (mModule != null)
            {
                FrmCard newFrm = asm.CreateInstance(numeClasa, true, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null,
                   new object[] { ModuleInfo }, Thread.CurrentThread.CurrentCulture, new object[] { }) as FrmCard;
                //newFrm.MdiParent = mdiForm;
                newFrm.ShowDialog();
            }
        }

        private void Inchidere_An(object sender, EventArgs e)
        {
            if (MessageBox.Show("Sunteti sigur ca doriti inchiderea anului curent?",
                            "Intrebare",
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Question) == DialogResult.No)
                return;
            if (MessageBox.Show("Documentele din anul curent se vor transfera in arhiva si vor fi vor putea fi doar consultate! Continuati?",
                            "Intrebare",
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Question) == DialogResult.No)
                return;
            SqlCommand cmd = null;
            MessageBox.Show("Atentie! Se vor inchide toate machetele. Salvati datele, acolo unde este cazul!",
                "Info",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);

            if (MdiParent!=null)
                foreach(StudioForm fChild in MdiParent.MdiChildren)
                {
                    if(fChild!=this)
                        fChild.Close();
                }

            try
            {
                Cursor.Current = Cursors.AppStarting;
                cmd = new SqlCommand("InchideAn", DeschideConexiune(workConnString));
                cmd.CommandTimeout = 500;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("pAn", ((DataRowView)balconSetupBindingSource.Current)["AnCurent"].ToString()));
                cmd.Parameters.Add(new SqlParameter("codUnit", ModuleInfo.CodUnitate.ToString()));
                cmd.ExecuteNonQuery();
                
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(),
                "Eroare",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
                return;
            }
            finally
            {
                InchideConexiune(cmd.Connection);
                Cursor.Current = Cursors.Default;
            }
            MessageBox.Show("Documentele curente au fost arhivate cu succes!",
                            "Info",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
        }

        override public void SetCurentMonth(short vCodUnitate)
        {
            
        }

        override public void SetCurentUnit(short vCodUnitate)
        {

            this.balconSetupTableAdapter.Connection = DeschideConexiune(workConnString);
            balconSetupTableAdapter.AddStatementCompleteHandler(Command_StatementCompleted);
            this.balconSetupTableAdapter.Fill(this.eSRPDataSet.BalconSetup, vCodUnitate);
            this.sectorBugetarTableAdapter.Connection = balconSetupTableAdapter.Connection;
            sectorBugetarTableAdapter.AddStatementCompleteHandler(Command_StatementCompleted);
            this.sectorBugetarTableAdapter.Fill(this.eSRPDataSet.SectorBugetar);

			this.surseFinantareTableAdapter.Connection = this.balconSetupTableAdapter.Connection;
            this.surseFinantareTableAdapter.AddStatementCompleteHandler(Command_StatementCompleted);
			this.surseFinantareTableAdapter.Fill(this.eSRPDataSet.SurseFinantare);
            if (balconSetupBindingSource.Count == 0)
            {
                balconSetupBindingSource.AllowNew = true;
                Adauga(this, null);
                balconSetupBindingSource.AllowNew = false;
                anCurentNUD.Enabled = true;
            }
            InchideConexiune(this.balconSetupTableAdapter.Connection);
        }
        /// <summary>
        /// cand se apasa enter, controlul activ este luna, anul
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        override public void toolStripLabelAnLuna_Click(object sender, EventArgs e)
        {

        }

        private void ArhivaEditabilaCB_CheckedChanged(object sender, EventArgs e)
        {
            CodSecretFrm frm = new CodSecretFrm();
            if (ArhivaEditabilaCB.Checked)
            {
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
                    ArhivaEditabilaCB.Checked = true;
                else
                    ArhivaEditabilaCB.Checked = false;
            }
        }

        public Boolean GetArhivaEditabila()
        {
            return ArhivaEditabilaCB.Checked;
        }

    }
}
